package com.app.megaprosoft.modyimcarcare;

import com.app.megaprosoft.DB.Ar;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class CarCare_List_Search extends Activity {
	private ListView lv;
	private EditText et;
	private String data[][];
	private ArrayAdapter<String> mArrayAdapter;
	int textlength = 0;
	SQLiteDBClass myDb;
	Ar ar = new Ar();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_carcare_activity_search);
		myDb = new SQLiteDBClass(this);
		ar.setContext(getApplicationContext());
		lv = (ListView) findViewById(R.id.ListView0111);
		et = (EditText) findViewById(R.id.EditText0111);
		mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
		lv.setAdapter(mArrayAdapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View selectedItemView, int position,
					long id) {
				// TODO Auto-generated method stub
//				et.setText(data[position][4].toString());
				Intent ett = new Intent(CarCare_List_Search.this,CarCare_GetCar.class);
				ett.putExtra("ArName", data[position][4].toString());
				startActivityForResult(ett, RESULT_OK);
				finish();
			}
		});
		
		
		try {
			data = ar.SelectAll_ArFF("","","");
			for (int i = 0; i < data.length; i++) {
				mArrayAdapter.add(data[i][4].toString());
				}
		}catch(Exception e){
			
		}
			
		et.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				// Abstract Method of TextWatcher Interface.
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// Abstract Method of TextWatcher Interface.
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				textlength = et.getText().length();
//				array_sort = new L<String>(CarCare_List_Search.this);
				mArrayAdapter.clear();
				try {
					data = ar.SelectAll_ArFF("","","");
					for (int i = 0; i < data.length; i++) {
//						mArrayAdapter.add(data[i][4].toString());
						if (textlength <= data[i][4].length()) {
							if (et.getText()
									.toString()
									.equalsIgnoreCase(
											(String) data[i][4].subSequence(
													0, textlength))) {
								mArrayAdapter.add(data[i][4].toString());
							}
						}
					}
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
	}
}